{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Path: cantaloupe.srv.cs.cmu.edu!crabapple.srv.cs.cmu.edu!bb3.andrew.cmu.edu!andrew.cmu.edu!cs6t+\\nFrom: \"Calvin D. Swartzentruber\" <cs6t+@andrew.cmu.edu>\\nNewsgroups: comp.sys.ibm.pc.hardware\\nSubject: ATTN: Ken Smith\\nDate: Mon,  5 Apr 1993 18:07:12 -0400\\nOrganization: Freshman, Electrical and Computer Engineering, Carnegie Mellon, Pittsburgh, PA\\nLines: 9\\nMessage-ID: <Qfk_qEi00WBOE8hm8Z@andrew.cmu.edu>\\nNNTP-Posting-Host: po3.andrew.cmu.edu\\nIn-Reply-To: <9304051540.AA03922@gumby.Altos.COM>\\n\\nIt is model number #7033D, a 14\" interlaced .28dp.  BTW, if you have a\\nnumber to contact the company, that would really be helpful to.  Thanks\\nfor replying.  I was beginning to believe that I was never going to get\\na reply.  I posted this on the netnews bboard because the first message\\nI sent to you was returned, and I didn\\'t know if my second message would\\nget to you.\\n\\nCalvin\\n\\n'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import re\n",
    "txt=''\n",
    "file=open('1')\n",
    "for i in file:\n",
    "    txt=txt+i\n",
    "file.close()\n",
    "\n",
    "txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#在txt中找到所有的to放入result\n",
    "result=re.findall('to',txt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['to', 'to', 'to', 'to', 'to', 'to', 'to']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#a[a-z]c\n",
    "#两个字符之间加个（），如‘a（）c’表示ac之间所有的东西"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Apr', 'and', 'and']"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#模糊匹配：以a开头的三个字母的单词，正则表达式中‘。’表示任意字符\n",
    "result1=re.findall(' ([Aa][a-z][a-z]) ',txt)\n",
    "result1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Apr', 'and'}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#去掉重复的结果\n",
    "result1=set(result1)\n",
    "result1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Apr', 'and', 'ave', 'act', 'ard', 'and', 'age']\n"
     ]
    }
   ],
   "source": [
    "#匹配多个空格‘ *’可以为0或者为n个字符  '+'表示至少要有一个字符\n",
    "result1=re.findall(' *([Aa][a-z][a-z]) ',txt)\n",
    "print(result1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('', 'Apr'), ('and', ''), ('', 'Alt'), ('and', '')]\n"
     ]
    }
   ],
   "source": [
    "result1=re.findall(' (a[a-z][a-z]) |(A[a-z][a-z])',txt)\n",
    "print(result1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['3', '6', '6', '5', '1993', '18', '07', '12', '0400', '9', '00', '8', '8', '3', '9304051540', '03922', '7033', '14', '28']\n"
     ]
    }
   ],
   "source": [
    "#找出所有的数字\n",
    "res=re.findall('\\d+',txt)\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['19', '93', '18', '07', '12', '04', '00', '00', '93', '04', '05', '15', '40', '03', '92', '70', '33', '14', '28']\n"
     ]
    }
   ],
   "source": [
    "#匹配到两个数字\n",
    "res=re.findall('\\d{2}',txt)\n",
    "#两个或者三个数字\n",
    "# res=re.findall('\\d{2，3}',txt)\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
